package com.gzhryc.shared_device.oem.h5.mng.controllers.pcl;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.data.dto.ListResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.common.device.dao.db.DeviceType;
import com.gzhryc.shared_device.common.device.services.DeviceTypeService;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceBusinessConfig;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlaceBusinessConfigService;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;
import com.gzhryc.shared_device.pcl.PCLSetMealCostContent;
import com.gzhryc.shared_device.pcl.code.dao.db.PCLFeeTemplate;
import com.gzhryc.shared_device.pcl.code.dao.enums.PCLFeeTemplateEnum;
import com.gzhryc.shared_device.pcl.code.services.PCLFeeTemplateService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class PCLDeviceCostController {

    @Route(value = { "/pcl/place/cost.html" })
    public String placeCost(HttpServletRequest request) {
        Long placeId = WebUtils.getLong(request,"placeId");
        PlacePhysicalStore physicalStore = PlacePhysicalStoreService.self().getById(placeId);
        request.setAttribute("placeName",physicalStore.getName());
        return "/pcl/cost/place/config.html";
    }

    @Route(value = { "/pcl/place/cost/item/list.data" })
    public ListResult<PCLSetMealCostContent.Item> placeCostItemListData(HttpServletRequest request) {
        ListResult<PCLSetMealCostContent.Item> result = new ListResult<>(DtoConstants.NULL_DATA,"暂无数据");
        Long placeId = WebUtils.getLong(request,"placeId");
        result.getBody().setData(new ArrayList<>());

        PlaceBusinessConfig businessConfig = PlaceBusinessConfigService.self().getByKeys(placeId, EBusinessType.PCL.index());
        if(businessConfig != null && StringTools.isNotBlank(businessConfig.getCostContent())) {
            PCLSetMealCostContent costContent = JsonTools.fromJson(businessConfig.getCostContent(), PCLSetMealCostContent.class);
            if (costContent != null && costContent.getItems() != null && costContent.getItems().size() > 0) {
                List<Long> feeTemplateIds = new ArrayList<>();
                for (PCLSetMealCostContent.Item item : costContent.getItems()) {
                    feeTemplateIds.add(item.getFeeTemplateId());
                }
                List<PCLFeeTemplate> feeTemplateList = PCLFeeTemplateService.self().findJsonByIds(feeTemplateIds);
                for(PCLSetMealCostContent.Item item : costContent.getItems()){
                    for(PCLFeeTemplate feeTemplate : feeTemplateList){
                        if(item.getDeviceTypeId().equals(feeTemplate.getDeviceTypeId())){
                            item.setDeviceTypeName(feeTemplate.getDeviceTypeName());
                            item.setFeeTemplateName(feeTemplate.getTitle());
                            break;
                        }
                    }
                    result.getBody().getData().add(item);
                }
            }
        }

        List<PCLFeeTemplate> feeTemplateList = PCLFeeTemplateService.self().findJoinDefPlatform();
        if(feeTemplateList != null && feeTemplateList.size() > 0) {
            for (PCLFeeTemplate feeTemplate : feeTemplateList) {
                boolean flag = true;
                for( PCLSetMealCostContent.Item item : result.getBody().getData()){
                    if(item.getDeviceTypeId().equals(feeTemplate.getDeviceTypeId())){
                        flag = false;
                        break;
                    }
                }
                if(flag) {
                    PCLSetMealCostContent.Item item = new PCLSetMealCostContent.Item();
                    item.setDeviceTypeId(feeTemplate.getDeviceTypeId());
                    item.setDeviceTypeName(feeTemplate.getDeviceTypeName());
                    item.setFeeTemplateId(feeTemplate.getId());
                    item.setFeeTemplateName(feeTemplate.getTitle());
                    result.getBody().getData().add(item);
                }
            }
        }

        if(result.getBody().getData().size() > 0){
            result.setCode(DtoConstants.SUCCESS,null);
        }

        return result;
    }

    @Route(value = { "/pcl/place/cost/change/list.html" })
    public String placeCostChange(HttpServletRequest request) {
        String deviceTypeId = WebUtils.getString(request,"deviceTypeId");
        DeviceType deviceType = DeviceTypeService.self().getById(deviceTypeId);
        request.setAttribute("deviceTypeName",deviceType.getName());
        return "/pcl/cost/place/change.html";
    }

    @Route(value = { "/pcl/place/cost/template/list.data" })
    public ListResult<PCLFeeTemplate> placeCostTemplateListData(HttpServletRequest request) {
        ListResult<PCLFeeTemplate> result = new ListResult<>(DtoConstants.NULL_DATA, "暂无数据");
        Long placeId = WebUtils.getLong(request,"placeId");
        String deviceTypeId = WebUtils.getString(request,"deviceTypeId");

        Long feeTemplateId = 0L;

        PlaceBusinessConfig businessConfig = PlaceBusinessConfigService.self().getByKeys(placeId, EBusinessType.PCL.index());
        if(businessConfig != null && StringTools.isNotBlank(businessConfig.getCostContent())) {
            PCLSetMealCostContent costContent = JsonTools.fromJson(businessConfig.getCostContent(), PCLSetMealCostContent.class);
            if (costContent != null && costContent.getItems() != null && costContent.getItems().size() > 0) {
                for (PCLSetMealCostContent.Item item : costContent.getItems()) {
                    if(item.getDeviceTypeId().equals(deviceTypeId)) {
                        feeTemplateId = item.getFeeTemplateId();
                        break;
                    }
                }
            }
        }

        List<PCLFeeTemplate> feeTemplateList = PCLFeeTemplateService.self().findJoinByDeviceTypeId(deviceTypeId);
        if(feeTemplateList != null && feeTemplateList.size() > 0){
            result.setCode(DtoConstants.SUCCESS,null);
            for(PCLFeeTemplate feeTemplate : feeTemplateList) {
                feeTemplate.setCurrentUse(false);
                if(feeTemplateId == 0L){
                    if(feeTemplate.getState().equals(PCLFeeTemplateEnum.State.Default.index())){
                        feeTemplate.setCurrentUse(true);
                    }
                }else {
                    if (feeTemplateId.equals(feeTemplate.getId())){
                        feeTemplate.setCurrentUse(true);
                    }
                }
            }
            result.getBody().setData(feeTemplateList);
        }
        return result;
    }

    @Route(value = { "/pcl/place/cost/template/change.do" })
    public AjaxResult doPlaceCostChange(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR,"设置失败");
        Long placeId = WebUtils.getLong(request,"placeId");
        Long feeTemplateId = WebUtils.getLong(request,"feeTemplateId");
        try {
            if(PlaceBusinessConfigService.self().updatePCLCostTemplate(placeId,feeTemplateId)){
                result.setCode(DtoConstants.SUCCESS,null);
            }
        } catch (LogicException e) {
            throw new RuntimeException(e);
        }
        return result;
    }
}
